﻿<?php
/* 
 * U-懒惰匹配 
 * i-忽略英文字母大小写 
 * x-忽略空白 
 * s-让元字符' . '匹配包括换行符内所有字符 
 */  
	$vid=$_GET['vid'];
	$type=$_GET['type'];
	$mode=$_GET['mode'];
	
	switch($type){
		case "youku":
			youku($vid);
		break;
		
		case "letv":
			letv($vid,$mode);
		break;
		
		case "iqiyi":
			iqiyi($vid,$mode);
		break;
		
		case "sohu":
			sohu($vid);
		break;
		
		case "bilibili":
			bilibili($vid,$mode);
		break;
		case "shouyoutan":
			shouyoutan($vid,$mode);
		break;
		default:
			echo "请输入正确参数 type(youku,letv,iqiyi),vid,mode(1,2,3,4...)";
		break;
	}
//Bilibili
	function bilibili($v,$m){
			//参数只能使用专辑id, (iconv处理bilibili乱码问题)
			if(!isset($m)||($m=='1')){
			$con=file_get_contents("http://bangumi.bilibili.com/jsonp/seasoninfo/".$v.".ver");}
			else if ($m=='2'){
				$con=file_get_contents("http://www.acglala.com/123gg.htm");}
			// echo $con;
			if ($con=="")
			{echo "get contents failed";}
			else{
			$con1=getNeedBetween($con,"seasonListCallback",'evaluate"');
			// echo $con1;
			$preg1.='#av_id":"(.*)"#iUs';
			$preg2.='#index":"(.*)"#iUs';
			preg_match_all($preg1,$con1,$arr1);
			preg_match_all($preg2,$con1,$arr2);

			//反向排序
			$re_arr1=array_reverse($arr1[1]);
			$re_arr2=array_reverse($arr2[1]);
			
			foreach($re_arr2 as $id=>$v){				
				echo "哔哩哔哩"."$"."第".$v."集"."$".$re_arr1[$id]."<br>";
			}
			}
			
	}
	
//优酷	
	function youku($v){
			$i=1;
			$con=file_get_contents("http://list.youku.com/show/episode?id=".explode("jQuery",$v)[0]."&stage=reload_".$i."&callback=jQuery".explode("jQuery",$v)[1]."");
			$j=substr_count($con,'title').'<br />';
			while($j>0){
				$con=file_get_contents("http://list.youku.com/show/episode?id=".explode("jQuery",$v)[0]."&stage=reload_".$i."&callback=jQuery".explode("jQuery",$v)[1]."");
				$newcon.=$con;
				$i=$i+10;
				$j=substr_count($con,'title').'<br />';
			}
			// echo $newcon;
			
			$con1=str_replace("\/","/",$newcon);
			$con2=str_replace('\"','"',$con1);
			// echo $con2;
			// die();
			// $con1=getNeedBetween($con,"<li clear='21'>",'<ul id="lb">');

			// $con1=trimall($con1);

			// echo $con1;
			$preg1.='#//v.youku.com/v_show/id_(.*).html"#iUs';
			$preg2.='#title="(.*)">#iUs';
			preg_match_all($preg1,$con2,$arr1);
			preg_match_all($preg2,$con2,$arr2);
			// print_r ($arr1);
			// print_r ($arr2);
			// die();
				foreach($arr2[1] as $id=>$v){
					echo "优酷"."$".unicode_decode($v)."$".$arr1[1][$id]."<br>";
				}
	}	
	
//搜狐
	function sohu($v){
			//参数只能使用专辑id, (iconv处理搜狐乱码问题)
			$con= iconv("gb2312", "utf-8//IGNORE",file_get_contents($v));
			$con1=getNeedBetween($con,"<li clear='21'>",'<ul id="lb">');

			$con1=trimall($con1);

			// echo $con1;
			$preg1.="#<strong><atarget=_blankhref='(.*)'#iUs";
			$preg2.='#>第(.*)集#iUs';
			preg_match_all($preg1,$con1,$arr1);
			preg_match_all($preg2,$con1,$arr2);
			//print_r ($arr1);
			//print_r ($arr2);
			// echo zz('#var vid="(.*)"#iUs',$urlcon);
			foreach($arr2[1] as $id=>$v){
				$urlcon=iconv("gb2312", "utf-8//IGNORE",file_get_contents($arr1[1][$id]));
				echo "搜狐"."$"."第".$v."集"."$".zz('#var vid="(.*)"#iUs',$urlcon)."<br>";
			}
	}	
	
//shouyoutan
	function shouyoutan($v,$m){
			if ($m=='2'){
				$con=file_get_contents("http://www.acglala.com/123gg.htm");}
			$con1=trimall($con);
			$preg1.='#<ahref="(.*)"#iUs';
			$preg2.='#title="(.*)"#iUs';
			preg_match_all($preg1,$con1,$arr1);
			preg_match_all($preg2,$con1,$arr2);
			$arr1=array_reverse($arr1[1]);
			$arr2=array_reverse($arr2[1]);
			// echo zz('#var vid="(.*)"#iUs',$urlcon);
			foreach($arr2 as $id=>$v){
				$urlcon=file_get_contents('http://www.shouyoutan.com'.$arr1[$id]);
				$res.="百度云"."|".$v."|".zz('#<p style="height:auto;"><a href="(.*)"#iUs',$urlcon)."<br>";
			}
		$res=str_replace("#密码: ","@@",$res);
		$res=str_replace("#密码：","@@",$res);
		echo $res;
	}	
	
	
	
	//乐视
	function letv($v,$m){
			if(!isset($m)||($m=='1')){
				//参数只能使用专辑id
				$con=file_get_contents("http://www.le.com/comic/".$v.".html");	
				$con1=getNeedBetween($con,"j_all_tuwen","j_all_juji");
				

				$preg1.='#<dd class="d_img"><a href="(.*)"#iUs';
				$preg2.='#>第(.*)集</a></dt>#iUs';
				preg_match_all($preg1,$con1,$arr1);
				preg_match_all($preg2,$con1,$arr2);

				foreach($arr2[1] as $id=>$v){
					preg_match('/\d+/',$arr1[1][$id],$levid);
					// echo $v."<br>";
					
					// echo $levid[0]."<br>";
					
					echo "乐视"."$"."第".$v."集"."$".$levid[0]."<br>";
				}
			}
			else if ($m=='2'){
				$con=file_get_contents("http://www.acglala.com/123gg.htm");
				$preg1.='#episode":"(.*)",#iUs';
				$preg2.='#,"vid":(.*),"#iUs';
				preg_match_all($preg1,$con,$arr1);
				preg_match_all($preg2,$con,$arr2);
				foreach($arr1[1] as $id=>$v){
					echo "乐视"."$"."第".$v."集$".$arr2[1][$id]."<br>";
				}
		
				
			}
	

	}
	
//爱奇艺
	function iqiyi($v,$m){
			//参数只能使用专辑id
			if(!isset($m)||($m=='1')){
				$con=file_get_contents($v);
				$con1=getNeedBetween($con,"<!-- 图文选集-->",'<a class="site-piclist_pic_link" href="{{item.vurl}}"');

			$con1=trimall($con1);
			
			$preg1.='#<aclass="site-piclist_pic_link"href="(.*)"#iUs';
			$preg2.='#">第(.*)集#iUs';
			preg_match_all($preg1,$con1,$arr1);
			preg_match_all($preg2,$con1,$arr2);
			//print_r ($arr1);
			
			//print_r ($arr2);

			foreach($arr2[1] as $id=>$v){
				// echo "爱奇艺"."$"."第".$v."集"."$".$arr1[1][$id]."<br>";
				$urlcon=file_get_contents($arr1[1][$id]);
				echo "爱奇艺"."$"."第".$v."集"."$".zz('#data-player-tvid="(.*)"#iUs',$urlcon)."_".zz('#data-player-videoid="(.*)"#iUs',$urlcon)."<br>";
			}
				
			}
			else if ($m=='2'){
				$con=file_get_contents("http://www.acglala.com/123gg.htm");
				$preg1.='#,"id":(.*),"#iUs';
				$preg2.='#shortTitle":"(.*)",#iUs';
				$preg3.='#vid":"(.*)",#iUs';
				preg_match_all($preg1,$con,$arr1);
				preg_match_all($preg2,$con,$arr2);
				preg_match_all($preg3,$con,$arr3);
				foreach($arr2[1] as $id=>$v){
					echo "爱奇艺"."$".$v."$".$arr1[1][$id]."_".$arr3[1][$id]."<br>";
				}
				
			}
			else if ($m=='3'){
				$con=file_get_contents("http://www.acglala.com/123gg.htm");
				$preg1.='#vurl":"(.*)"#iUs';
				$preg2.='#shortTitle":"(.*)",#iUs';
				preg_match_all($preg1,$con,$arr1);
				preg_match_all($preg2,$con,$arr2);
				foreach($arr2[1] as $id=>$v){
				echo "爱奇艺"."$".$v."$".$arr1[1][$id]."<br>";
			}
				
			}

	}
	
function trimall($str)//删除空格
{
    $qian=array(" ","　","\t","\n","\r");$hou=array("","","","","");
    return str_replace($qian,$hou,$str);    
}
function zz($preg,$con,$num=1){
	preg_match($preg,$con,$arr);
	return $arr[$num];
}

	
	
	// //优酷
	// function youku($v,$m){
		// $num = 99;
		// $c = array();
		// for ($i = 0; $i < $num; $i++) {
			// $list_video = get_data($v, cj_data($v));
			// if ($list_video['state'] == 2) {
				// break;
			// } 
			// else {
				// $c = array_merge($c, $list_video);
				// $v = end($list_video);
			// }
		// }
		// // print_r($c);
		// if((!isset($m))||$m=='1'){
			// $i=1;
			// foreach ($c as $key => $value) {
			// $d[] = '优酷$第' . $i++ . '集$' . $value;
		// }
		// echo $list = implode("<br>", $d);
		// }
		// else if($m==2){
			// foreach ($c as $key => $value) {
				// $strkey=strstr($key," ",true);
			// $d[] = '优酷$' . $strkey . '$' . $value;
		// }
		// echo $list = implode("\r\n", $d);
		// }
		// // echo $list = implode("\r\n", $d);
		// //echo $list = implode("<br>", $d);
	// }

	// function get_data($vid, $cj_param) {
		// $cj_url = 'https://ups.youku.com/ups/get.json';
		// // $cj_param = cj_data($vid);
		
		// $url_data = curl($cj_url, 'GET', '', $cj_param, '');
		// // print_r($url_data);die;
		// if(empty($url_data)) {
			// $a = array(
				// 'state' => '2',
				// 'msg' => '执行完毕!',
				// 'list' => $a
			// );
		// }
		
		// $json = json_decode($url_data, 1);
		// $a = i_array_column($json['data']['videos']['list'], 'encodevid', 'title');
		// if (end($a) == $vid) {
			// $a = array(
				// 'state' => '2',
				// 'msg' => '执行完毕!',
				// 'list' => $a
			// );
		// }
		// return $a;
	// }
	// function cj_data($_vid) {
		// $_param = array(
			// "vid" => $_vid,
			// "ccode" => "0402",
			// "client_ip" => "192.168.1.1",
			// "utid" => "FMTeEGRf3CMCAdpS1nOPcNxP",
			// "client_ts" => "",
			// "playlist_id" => "null",
			// "ob" => "null",
			// "qq-pf-to" => "pcqq.c2c"
		// );
		// return $_param;
	// }
	// function i_array_column($input, $columnKey, $indexKey=null){
		 // if(!function_exists('array_column')){ 
			 // $columnKeyIsNumber  = (is_numeric($columnKey))?true:false; 
			 // $indexKeyIsNull            = (is_null($indexKey))?true :false; 
			 // $indexKeyIsNumber     = (is_numeric($indexKey))?true:false; 
			 // $result                         = array(); 
			 // foreach((array)$input as $key=>$row){ 
				 // if($columnKeyIsNumber){ 
					 // $tmp= array_slice($row, $columnKey, 1); 
					 // $tmp= (is_array($tmp) && !empty($tmp))?current($tmp):null; 
				 // }else{ 
					 // $tmp= isset($row[$columnKey])?$row[$columnKey]:null; 
				 // } 
				 // if(!$indexKeyIsNull){ 
					 // if($indexKeyIsNumber){ 
					   // $key = array_slice($row, $indexKey, 1); 
					   // $key = (is_array($key) && !empty($key))?current($key):null; 
					   // $key = is_null($key)?0:$key; 
					 // }else{ 
					   // $key = isset($row[$indexKey])?$row[$indexKey]:0; 
					 // } 
				 // } 
				 // $result[$key] = $tmp; 
			 // } 
			 // return $result; 
		 // }else{
			 // return array_column($input, $columnKey, $indexKey);
		 // }
	 // }
	// /**
	 // * CURL封装
	 // * @param mixed  $url 		采集页面地址
	 // * @param string $curl_type 采集方式 GET|POST|DELETE|PUT
	 // * @param string $referer   采集页面地址
	 // * @param array	 $data   	GET或POST数据数组
	 // * @param array	 $headers  	发送页面头部信息
	 // * @return string
	 // */
	// function curl($url, $curl_type, $referer, $data = array() , $headers = array()) {
		// $_headers = array();
		// $_timeout = 30; //超时
		// if (!empty($headers)) {
			// foreach ($headers as $key => $val) {
				// $_headers[] = $key . ':' . $val;
			// }
		// }
		// // $_ip= getIP();
		// $curl = curl_init();
		// curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); //让cURL自己判断使用哪个版本
		// // curl_setopt($curl, CURLOPT_MUTE, 1);
		// curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
		// if (!empty($referer)) curl_setopt($curl, CURLOPT_REFERER, $referer);
		// // if(!empty($referer)) curl_setopt($curl, CURLOPT_AUTOREFERER, true);
		// curl_setopt($curl, CURLOPT_HEADER, false);
		// curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $_timeout);
		// curl_setopt($curl, CURLOPT_HTTPHEADER, $_headers);
		// curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		
		// curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
		// curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
		
		// switch (strtoupper($curl_type)) {
			// case 'GET':
				// if (!empty($data)) $url.= '?' . http_build_query($data);
				// break;

			// case 'POST':
				// curl_setopt($curl, CURLOPT_POST, TRUE);
				// if (!empty($data)) curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
				// break;

			// case 'DELETE':
				// curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
				// if (!empty($data)) $url.= '?' . http_build_query($data);
				// break;

			// case 'PUT':
				// curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
				// if (!empty($data)) curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
				// break;

			// default:
				// curl_close($curl);
				// die('HTTP request mode does not exist!');
			// }
			// curl_setopt($curl, CURLOPT_URL, $url);
			// $response = curl_exec($curl);
			// $err = curl_errno($curl);
			// $info = curl_getinfo($curl);
			// //print_r($info);
			// curl_close($curl);
			// //字符串是否为xml,是就解析解析成数组，否则返回原数据
			// if ($err) return $err;
			// // return xml_parser($response) !== FALSE ? xml_parser($response) : $response;
			// return $response;
		// }

	// function FetchRepeatMemberInArray($array) { 
    // // 获取去掉重复数据的数组 
    // $unique_arr = array_unique ( $array ); 
    // // 获取重复数据的数组 
    // $repeat_arr = array_diff_assoc ( $array, $unique_arr ); 
    // return $repeat_arr; 
// } 
	

	//截取中间内容函数
	function getNeedBetween($kw1,$mark1,$mark2){
		 $kw=$kw1;
		 $kw='123'.$kw.'123';
		 $st =stripos($kw,$mark1);
		 $ed =stripos($kw,$mark2);
		 if(($st==false||$ed==false)||$st>=$ed)
		 return 0;
		 $kw=substr($kw,($st+1),($ed-$st-1));
		 return $kw;
	}
	
	// 将UNICODE编码后的内容进行解码
	function unicode_decode($name)
	{
	  // 转换编码，将Unicode编码转换成可以浏览的utf-8编码
	  $pattern = '/([\w]+)|(\\\u([\w]{4}))/i';
	  preg_match_all($pattern, $name, $matches);
	  if (!empty($matches))
	  {
		$name = '';
		for ($j = 0; $j < count($matches[0]); $j++)
		{
		  $str = $matches[0][$j];
		  if (strpos($str, '\\u') === 0)
		  {
			$code = base_convert(substr($str, 2, 2), 16, 10);
			$code2 = base_convert(substr($str, 4), 16, 10);
			$c = chr($code).chr($code2);
			$c = iconv('UCS-2', 'UTF-8', $c);
			$name .= $c;
		  }
		  else
		  {
			$name .= $str;
		  }
		}
	  }
	  return $name;
	}
	
?>